package myJs

import myJs.myPkg.bootstrap.Bootstrap.default._
import myJs.Utils.{layer}
import scala.scalajs.js.Dynamic.{global=>g}
import myJs.myPkg.{Swal, SwalOptions}
import myJs.myPkg.jquery._
import scalatags.Text.all.s
import shared.VarTool

import scala.scalajs.js
import scala.scalajs.js.annotation.{JSExport, JSExportTopLevel}

/**
 * Created by yz on 2019/4/25
 */
@JSExportTopLevel("App")
object App {

  @JSExport("init")
  def init = {
    val shareTitle = VarTool.siteName
    val beforeTitle = $("#shareTitle").text()
    $("#shareTitle").text(s"${beforeTitle}-${shareTitle}")
    getDiskSpace

  }

  def getDiskSpace = {
    val url = g.jsRoutes.controllers.AppController.getProjectSpace().url.toString
    val ajaxSettings = JQueryAjaxSettings.url(url).`type`("get").success { (data, status, e) =>
      val rs = data.asInstanceOf[js.Dictionary[Double]]
      val used = rs("used").toDouble
      val total = rs("total").toDouble
      val percent = (used * 100 / total)
      $("#spaceHoverInfo").attr("title", s"已使用${used}GB,总共${total}GB")
      $("#spaceInfo").text(s"${used}G/${total}G")
      val backgoundColor = percent match {
        case x if x <= 50 => "#3C82BE"
        case x if x <= 70 => "#5DBFD8"
        case x if x <= 90 => "#EEAD4F"
        case x if x <= 100 => "#DE534E"
      }
      $("#spaceBar").css("width", s"${percent}%").css("background-color", backgoundColor)

    }
    $.ajax(ajaxSettings)
  }

}
